﻿using System;
using BO.Custom;
using Business;
using Entity;
using FreshCms.BackEnd.ControlsCommon;
using Utilities;


namespace FreshCms.BackEnd.Controls.Systems.User
{
    public partial class UserDetail : System.Web.UI.UserControl
    {
        public Int32 id = Utils.GetQuery("id", 0);
        public string pageTitle = string.Empty;
        public string iSuperAdmin = "";
        public string btnSubmitText = string.Empty;
        public InUser obj = new InUser();

        #region check login and permission
        InPms chkPms = new InPms();
        public Int32 panelId = 0;
        private void checkLogin()
        {
            // Check and load UC
            if (Request.QueryString["ui"] != null)
                Int32.TryParse(Request.QueryString["ui"], out panelId);
            // check Pms
            if (panelId > 0)
                chkPms = InPmsBusiness.CheckSessionPms(panelId);
            if (chkPms == null)
                Response.Redirect("/BackEnd/xlogin.aspx?redirect=" + Utils.GetCurrentCmsPage().Replace("?", "@").Replace("&", "|"));
            // show Pms in Top Bar
            TopBarPms ucTopBar = Page.Master.FindControl("TopBarPms") as TopBarPms;
            ucTopBar.BindData(chkPms);
        }
        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {
            checkLogin();
            if (!Page.IsPostBack)
            {
                if (chkPms.PmsView == 1)
                {
                    BindGroup();
                    BindStatus();
                    BindData();
                }
            }
                
        }

        private void BindGroup()
        {
            ddlGroup.DataSource = InGroupBusiness.GetInGroupList();
            ddlGroup.DataTextField = "GroupName";
            ddlGroup.DataValueField = "GroupId";
            ddlGroup.DataBind();
        }

        private void BindStatus()
        {
            ddlStt.DataSource = StatusBusiness.GetShortListStatus(false);
            ddlStt.DataTextField = "Value";
            ddlStt.DataValueField = "Id";
            ddlStt.DataBind();
            ddlStt.Items[0].Attributes.Add("style", Constant.StyleDropDisable);
            ddlStt.Items[1].Attributes.Add("style", Constant.StyleDropEnable);
        }

        private void BindData()
        {
            if(id==0)
            {
                obj.UserAvatar = "/BackEnd/img/noavatar.jpg";
                pageTitle = "Thêm mới người dùng";
                btnSubmitText = "Thêm mới";
                phAddnew.Visible = true;
                return;
            }
            obj = InUserBusiness.GetInUserById(id);
            InUser currentUser = InUserBusiness.GetSessionInUser();
            if(obj!=null)
            {
                if (currentUser.UserId == id)
                    phUpdateMySeft.Visible = true;
                else
                    phUpdate.Visible = true;    
                pageTitle = "Thông tin tài khoản: \"" + obj.UserMail + "\"";
                btnSubmitText = "Cập nhật";
                ddlGroup.SelectedValue = obj.GroupId.ToString();
                ddlStt.SelectedValue = obj.UserStatus.ToString();
                if (obj.IsSuperAdmin == 1) iSuperAdmin = "checked";
            }
            
        }
    }
}